//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.未分类.demo1AND20;

public class demo6 {
    public demo6() {
    }

    public static void main(String[] args) {
        System.out.println(convert("PAYPALISHIRING", 4));
    }

    public static String convert(String s, int numRows) {
        if (s.isEmpty()) {
            return "";
        } else if (s.length() != 1 && s.length() > numRows && numRows != 1) {
            StringBuilder sb = new StringBuilder();
            int Addnum = (numRows - 1) * 2;
            int index = 0;

            for(int i = 0; i < numRows; ++i) {
                index = i;
                if (i != 0 && i != numRows - 1) {
                    while(index < s.length()) {
                        sb.append(s.charAt(index));
                        sb.append(index + Addnum < s.length() ? s.charAt(index + Addnum) : "");
                        index += (numRows - 1) * 2;
                    }
                } else {
                    while(index < s.length()) {
                        sb.append(s.charAt(index));
                        index += (numRows - 1) * 2;
                    }
                }

                if (Addnum == 2) {
                    Addnum = (numRows - 1) * 2;
                } else {
                    Addnum -= 2;
                }
            }

            return sb.toString();
        } else {
            return s;
        }
    }
}
